package com.example.zy.car.view.camera;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zy.car.R;
import com.example.zy.car.custom.WaterImageView;
import com.example.zy.car.utils.MyUtil;

import java.io.IOException;

import butterknife.Bind;
import butterknife.ButterKnife;

public class PhotoShowActivity extends Activity implements View.OnClickListener {

    @Bind(R.id.tv_left_save)
    TextView mTvLeftSave;
    @Bind(R.id.tv_left_cancle)
    TextView mTvLeftCancle;
    @Bind(R.id.tv_left_add_waterpic)
    TextView mTvLeftAddWaterpic;
    @Bind(R.id.tv_left_add_quexian)
    TextView mTvLeftAddQuexian;
    @Bind(R.id.menu_linearlayout)
    LinearLayout mMenuLinearlayout;
    @Bind(R.id.iv_photo)
    ImageView mIvPhoto;
    @Bind(R.id.rl_waterlayout)
    RelativeLayout mRlWaterlayout;
//    private ImageView photoImage;
//    private RelativeLayout waterLayout;
    private String photoPath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏  
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_photo_show);
        ButterKnife.bind(this);
        mTvLeftSave.setOnClickListener(this);
        mIvPhoto = (ImageView) findViewById(R.id.iv_photo);
        mTvLeftAddWaterpic.setOnClickListener(this);
//        waterLayout = (RelativeLayout) findViewById(R.id.rl_waterlayout);
        photoPath = getIntent().getStringExtra("PhotoPath");
        if (photoPath != null) {
            Bitmap bitmap = BitmapFactory.decodeFile(photoPath);
            mIvPhoto.setImageBitmap(bitmap);
            mIvPhoto.setOnClickListener(this);
        }
    }
   
    //要加水印的底图只有一张，所有position从1开始
    private int mPosition = 0;
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.tv_left_add_waterpic:
                if (photoPath != null) {
                    Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.type3);
                    mPosition = mPosition + 1;
                    WaterImageView imageView = new WaterImageView(PhotoShowActivity.this,
                            waterBitmap, ondeleteClick);
                    imageView.setmPosition(mPosition);
                    imageView.setId(mPosition);
                    imageView.setIsClick(1);
                    RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                    param.setMargins(10, 0, 0, 10);
                    imageView.setLayoutParams(param);
                    mRlWaterlayout.addView(imageView, param);
                }
                break;
            case R.id.tv_left_save:
                int count = mRlWaterlayout.getChildCount();
                for (int i = 0; i < count; i++) {
                    View view = mRlWaterlayout.getChildAt(i);
                    if (i != 0) {
                        WaterImageView view2 = (WaterImageView) view;
                        view2.setIsClick(0);
                        view2.invalidate();
                    }
                }
                String filePath = Environment
                        .getExternalStorageDirectory().getAbsolutePath() + "/test/save.jpg";
                Bitmap bitmap = MyUtil.getScreenShot(mRlWaterlayout);
                try {
                    MyUtil.writeBitmap(filePath, bitmap);
                    Intent intent = getIntent();
                    Bundle bundle = new Bundle();
                    bundle.putString("filePath", filePath);
                    intent.putExtra("bundle", bundle);
                    PhotoShowActivity.this.setResult(2, intent);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_left_cancle:
                finish();
                overridePendingTransition(R.anim.extre_in,R.anim.extre_out);
                break;
            case R.id.tv_left_add_quexian:
                AddTagActivity addTagActivity=new AddTagActivity();
                break;
            default:
                break;
        }
    }

    public WaterImageView.OnDeleteClick ondeleteClick = new WaterImageView.OnDeleteClick() {
        @Override
        public void delteClick(int position) {
            int count = mRlWaterlayout.getChildCount();
            for (int i = 0; i < count; i++) {
                View view = mRlWaterlayout.getChildAt(i);
                int id = view.getId();
                if (position == id) {
                    mRlWaterlayout.removeViewAt(i);
                    return;
                }
            }
        }
        @Override
        public void selectedClick(int position) {
            int count = mRlWaterlayout.getChildCount();
            for (int i = 0; i < count; i++) {
                View view = mRlWaterlayout.getChildAt(i);
                int id = view.getId();
                //0 代表要添加水印的图片
                if (i != 0) {
                    WaterImageView view2 = (WaterImageView) view;
                    if (position == id) {
                        //1表示需要画边框，以及两个按钮图片
                        view2.setIsClick(1);
                    } else {
                        view2.setIsClick(0);
                    }
                    view2.invalidate();
                }
            }
        }
    };
}
